package com.bjgov.scoresys.dao;
// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.bjgov.scoresys.entity.User;
import com.bjgov.scoresys.util.Page;

/**
 * A data access object (DAO) providing persistence and search support for User
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see .User
 * @author MyEclipse Persistence Tools
 */

public class UserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserDAO.class);

	public User getUserByName(String userName) {
    	User user = null;
    	List<User> users = findByProperty("loginName", userName);
    	if(users.size()>0) {
    		user = users.get(0);
    		return user;
    	}
    	return null;
    }		
	
	public void save(User transientInstance) {
		log.debug("saving User instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(User persistentInstance) {
		log.debug("deleting User instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public User findById(java.lang.Integer id) {
		log.debug("getting User instance with id: " + id);
		try {
			User instance = (User) getSession().get("com.bjgov.scoresys.entity.User", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<User> findByIds(String uids) {
		log.debug("finding User instance by ids");
		try {
			String queryString = "from User as model where model.id in(" + uids + ")";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by ids failed", re);
			throw re;
		}
	}	

	public List<User> findByProperty(String propertyName, Object value) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from User as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List<User> findByAuthority(Object authority) {
		return findByProperty("authority", authority);
	}	
	
	@Override
	public int count() {
		log.debug("getUserCount");
		try {
			String queryString = "select count(id) from User";
			Query query = getSession().createQuery(queryString);
	        return (Integer) query.iterate().next();
		} catch (RuntimeException re) {
			log.error("getUserCount failed", re);
			throw re;
		}
	}

	@Override
	public List getByPage(Page page) throws HibernateException {
		String querySentence = "from User";
		Query query = getSession().createQuery(querySentence);
		// 实现分页功能
		query.setFirstResult(page.getBeginIndex())
		.setMaxResults(page.getEveryPage());
		return query.list();
	}

}